package com.ruoyi.cngj.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cngj.domain.CngjBanner;
import com.ruoyi.cngj.service.ICngjBannerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 轮播图Controller
 * 
 * @author yuexl
 * @date 2024-06-27
 */
@Controller
@RequestMapping("/cngj/banner")
public class CngjBannerController extends BaseController
{
    private String prefix = "cngj/banner";

    @Autowired
    private ICngjBannerService cngjBannerService;

    //@RequiresPermissions("cngj:banner:view")
    @GetMapping()
    public String banner()
    {
        return prefix + "/banner";
    }

    //@RequiresPermissions("cngj:banner:view")
    @GetMapping("/banner2")
    public String banner2()
    {
        return prefix + "/banner2";
    }

    @GetMapping("/main")
    public String main()
    {
        return prefix + "/main";
    }

    /**
     * 查询轮播图列表
     */
    //@RequiresPermissions("cngj:banner:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CngjBanner cngjBanner)
    {
        startPage();
        List<CngjBanner> list = cngjBannerService.selectCngjBannerList(cngjBanner);
        return getDataTable(list);
    }

    /**
     * 导出轮播图列表
     */
    //@RequiresPermissions("cngj:banner:export")
    @Log(title = "轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CngjBanner cngjBanner)
    {
        List<CngjBanner> list = cngjBannerService.selectCngjBannerList(cngjBanner);
        ExcelUtil<CngjBanner> util = new ExcelUtil<CngjBanner>(CngjBanner.class);
        return util.exportExcel(list, "轮播图数据");
    }

    /**
     * 新增轮播图
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增轮播图
     */
    @GetMapping("/add2")
    public String add2()
    {
        return prefix + "/add2";
    }

    /**
     * 新增保存轮播图
     */
    //@RequiresPermissions("cngj:banner:add")
    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CngjBanner cngjBanner)
    {
        return toAjax(cngjBannerService.insertCngjBanner(cngjBanner));
    }

    /**
     * 修改轮播图
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CngjBanner cngjBanner = cngjBannerService.selectCngjBannerById(id);
        mmap.put("cngjBanner", cngjBanner);
        return prefix + "/edit";
    }

    /**
     * 修改保存轮播图
     */
    //@RequiresPermissions("cngj:banner:edit")
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CngjBanner cngjBanner)
    {
        return toAjax(cngjBannerService.updateCngjBanner(cngjBanner));
    }

    /**
     * 删除轮播图
     */
    //@RequiresPermissions("cngj:banner:remove")
    @Log(title = "轮播图", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cngjBannerService.deleteCngjBannerByIds(ids));
    }

    /**
     * 改变位置
     */
    @Transactional
    @PostMapping("/bannerMove")
    @ResponseBody
    public AjaxResult bannerMove(String type,String id,String changeId)
    {
        CngjBanner cngjBanner = cngjBannerService.selectCngjBannerById(id);
        CngjBanner changeCngiBanner = cngjBannerService.selectCngjBannerById(changeId);
        int num =0;
        if("1".equals(type) || "2".equals(type)){
            //上移
            int currSort = cngjBanner.getBannerSort();
            cngjBanner.setBannerSort(changeCngiBanner.getBannerSort());
            changeCngiBanner.setBannerSort(currSort);
            num +=cngjBannerService.updateCngjBanner(cngjBanner);
            num +=cngjBannerService.updateCngjBanner(changeCngiBanner);
        }
        if("3".equals(type)){
            cngjBanner.setBannerSort(Integer.parseInt(changeId)+1);
            num +=cngjBannerService.updateCngjBanner(cngjBanner);
        }
        return toAjax(num);
    }

    /**
     * 启用停用banner素材
     */
    @Log(title = "启用停用banner素材")
    @PostMapping( "/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(String id,String bannerState)
    {
        //判断如果是首页轮播最少保留两张
        CngjBanner cngjBanner3 = new CngjBanner();
        cngjBanner3.setBannerType("1");
        cngjBanner3.setBannerState("1");
        List<CngjBanner> cngjBannerList = cngjBannerService.selectCngjBannerList(cngjBanner3);
        CngjBanner cngjBanner = new CngjBanner();
        cngjBanner.setId(id);
        CngjBanner cngjBanner1 = cngjBannerService.selectCngjBannerById(id);
        //判断如果是首页轮播最少保留两张
        if("1".equals(cngjBanner1.getBannerType()) && "0".equals(bannerState)){
            if(cngjBannerList.size()>2){
                cngjBanner.setBannerState(bannerState);
                return new AjaxResult(AjaxResult.Type.SUCCESS,"修改成功!",cngjBannerService.updateCngjBanner(cngjBanner));
            }else{
                return new AjaxResult(AjaxResult.Type.ERROR,"首页轮播至少要保留两张!");
            }
        }else{
            cngjBanner.setBannerState(bannerState);
            return new AjaxResult(AjaxResult.Type.SUCCESS,"修改成功!",cngjBannerService.updateCngjBanner(cngjBanner));
        }
    }
}
